<template>
	<view>
		<form>
			<view class="padding-sm">
				<view class="cu-form-group">
					<view class="title">商品</view>
					<button class='cu-btn bg-green shadow' @click="selectGoods">选择商品</button>
				</view>
				<view v-if="form.goods" class="bg-white padding-left-sm" style="margin-top: 1rpx;margin-bottom: 1rpx;">
					<view class="flex">
						<view class="flex-sub"><text>名称</text></view>
						<view class="flex-sub text-center"><text>调货价</text></view>
						<view class="flex-sub text-center"><text>数量</text></view>
					</view>
					<view v-for="(item,index) in form.goods" class="flex">
						<view class="flex-sub"><text>{{item.name}}</text>-<text>{{item.specifications}}</text></view>
						<view class="flex-sub text-center"><text>{{(item.transfer_price/100).toFixed(2)}}</text></view>
						<view class="flex-sub text-center">
							<input type="number" v-model="item.count" @input="goodsCountChange"></input>
						</view>
					</view>
				</view>
				<view class="cu-form-group">
					<view class="title">调货价</view>
					<input disabled="true" name="input" placeholder="调货价格" type="digit" v-model="form.money"></input>
				</view>
				<view class="cu-form-group ">
					<view class="title">说明</view>
					<input name="input" placeholder="请输入说明(非必填)" v-model="form.remark"></input>
				</view>
			</view>
		</form>
		<qianziyu-select :show="goodsShow" type="radio" popupTitle="选择商品" :dataLists="goodsList" name="title"
			placeholder="输入商品名称搜索" @cancel="goodsShow=false" @search="selectSearch" @submit="submitGoodsSelect">
		</qianziyu-select>
		<view class="padding">
			<button @click="submit" class="cu-btn block bg-blue margin-tb-sm lg">提交</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				goodsName: "",
				goodsList: [],
				goodsShow: false,
				modalName: '',
				checkData: true,
				required: false,
				content_title: '',
				form: {
					type: 2,
					content: []
				}
			}
		},
		onLoad(e) {

		},
		methods: {
			selectGoods() {
				this.goodsShow = true
			},
			async getGoods() {
				this.$func.wmzmall.call('transfer/goods', {
					name: this.goodsName
				}).then(res => {
					if (res.code === 200) {
						if (res.datas.data.length > 0) {
							this.goodsList = res.datas.data
						} else {
							this.$api.msg('暂无数据')
						}
					} else {
						this.$api.msg(res.msg)
					}
				});
			},
			selectSearch(e) {
				this.goodsName = e
				this.getGoods()
			},
			submitGoodsSelect(item) {
				let list = []
				list.push(item)
				this.form.goods = list
				let money = 0
				for (var i in list) {
					money = money + list[i].transfer_price
				}
				this.form.money = (money / 100).toFixed(2)
				this.goodsShow = false
			},
			goodsCountChange(item) {
				let list = this.form.goods
				let money = 0
				for (var i in list) {
					money = money + list[i].transfer_price * list[i].count
				}
				this.form.money = (money / 100).toFixed(2)
			},
			async submit() {
				if (!this.form.goods) {
					this.$api.msg('请选择商品')
					return
				}
				try {
					let params = this.form
					params.type = 0
					this.$func.wmzmall.call('transfer/add', params).then(res => {
						if (res.code === 200) {
							this.$api.msg('发布成功')
							uni.navigateBack()
						} else {
							this.$api.msg('发布失败,请稍后重试!!!')
						}
					});
				} catch (e) {
					this.$api.msg(e.errMsg)
				}
			}
		}
	}
</script>

<style>

</style>